package com.xmall.controller.admin;

import com.xmall.bean.vo.BaseRespVo;
import com.xmall.bean.vo.ListDataVo;
import com.xmall.bean.table.Brand;
import com.xmall.service.MallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("admin/brand")
public class BrandController {
    @Autowired
    MallService mallService;

    @RequestMapping("list")
    public BaseRespVo showBrandList(Integer page, Integer limit, String sort, String order, Integer id, String name) {
        ListDataVo listDataVo = mallService.showBrandList(page, limit, sort, order, id, name);
        return BaseRespVo.ok(listDataVo);
    }

    @RequestMapping("create")
    @Transactional
    public BaseRespVo addBrand(@RequestBody Brand brand) {
        Brand brandVO = mallService.addBrand(brand);
        return BaseRespVo.ok(brandVO);
    }

    @RequestMapping("delete")
    public BaseRespVo deleteBrand(@RequestBody Brand brand) {
        brand.setDeleted(true);
        Integer integer = mallService.updateBrand(brand);
        if (integer != 1){
            return BaseRespVo.fail();
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("update")
    public BaseRespVo updateBrand(@RequestBody Brand brand){
        mallService.updateBrand(brand);
        return BaseRespVo.ok(brand);
    }

}
